<template>
	<view class="shop-container">
		<!-- 搜索栏 -->
		<view class="search-bar">
			<view class="search-input-wrapper" @tap="goToSearch">
				<u-icon name="search" size="20"></u-icon>
				<input type="text" placeholder="搜索产品" readonly v-model="searchValue" />
			</view>
		</view>

		<!-- 分类导航 -->
		<view class="category-nav">
			<scroll-view scroll-x="true" class="scroll-view" show-scrollbar="false">
				<view class="category-item" v-for="(item, index) in categories" :key="index"
					:class="{ active: currentCategory === index }" @tap="selectCategory(index)">
					{{ item.name }}
				</view>
			</scroll-view>
		</view>

		<!-- 商品列表 -->
		<scroller @init="initScroller" @down="refreshData" @up="queryGoodsList" :up="optUp" :fixed="false">
			<view class="product-grid">
				<view class="product-item" v-for="(product, index) in products" :key="index"
					@tap="goGoodDetail(product)">
					<img :src="product.image | avatarUrlFind('1')" :alt="product.drugName" />
					<view class="product-info">
						<view class="product-title">{{ product.drugName }}</view>
						<view class="product-specs">{{ product.productInfo }}</view>
						<view class="price-sales-row">
							<view class="product-price" v-if="product.goodsCategory==='P'">¥{{ product.salesPrice }}
							</view>
							<view class="product-price" v-if="product.goodsCategory==='TC'">¥{{ product.packagePrice }}
							</view>
							<view class="product-sales">已售{{ product.salesNum }}+</view>
						</view>
					</view>
				</view>
			</view>
		</scroller>

	</view>
</template>

<script>
	import scroller from '@/components/scroller/scroller.vue';
	import {
		queryGoodsList,
		queryGoodsTypeList
	} from '@/api/shop.js'
	export default {
		head: {
			link: [{
				rel: 'stylesheet',
				href: 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'
			}]
		},
		data() {
			return {
				currentCategory: 0,
				searchValue: '', // 搜索关键词
				scroller: {},
				optUp: {
					onScroll: true, //监听滚动事件
					page: {
						size: 5
					} //下拉加载15条
				},
				queryParams: {},
				categories: [
					// {
					// 	name: '推荐'
					// },
					// {
					// 	name: '智能设备'
					// },
					// {
					// 	name: '中药餐包'
					// },
					// {
					// 	name: '西药问诊'
					// },
					// {
					// 	name: '本草纲目'
					// },
					// {
					// 	name: '养生保健'
					// },
					// {
					// 	name: '医疗器械'
					// },
					// {
					// 	name: '营养保健'
					// },
					// {
					// 	name: '母婴健康'
					// },
					// {
					// 	name: '美容养颜'
					// }
				],
				products: []
			}
		},
		created() {
			this.queryGoodsTypeList()
		},
		onLoad(options) {
			// 接收搜索关键词参数
			if (options.searchValue) {
				this.searchValue = decodeURIComponent(options.searchValue)
				// 执行搜索
				this.loadData()
			}
		},
		onShow() {
			// 检查全局数据中是否有搜索关键词
			const app = getApp()
			if (app.globalData && app.globalData.hasOwnProperty('searchValue')) {
				// 设置搜索值（可能为空字符串）
				this.searchValue = app.globalData.searchValue
				// 清空全局数据中的搜索关键词，避免重复搜索
				app.globalData.searchValue = ''
				// 执行搜索
				this.loadData()
			}
		},
		methods: {

			/*初始化滚动*/
			initScroller(scroller) {
				this.scroller = scroller;
			},
			/*刷新数据*/
			refreshData() {
				this.loadData()
			},
			/*加载数据*/
			loadData() {
				this.products = [];
				// 清空查询参数中的搜索关键词（除了当前搜索）
				if (!this.searchValue) {
					delete this.queryParams.drugName;
				}
				this.scroller.resetUpScroll();
			},


			selectCategory(index) {
				this.currentCategory = index;
				this.drugType = this.categories[index].id
				if(index===0){
					this.drugType=''
				}
				this.loadData()
			},
			goGoodDetail(product) {
				uni.navigateTo({
					url: '/subPackage_1/pages/goods/good-detail?id=' + product.id
				});
			},
			goToSearch() {
				uni.navigateTo({
					url: '/pages/shop/search'
				});
			},
			queryGoodsTypeList() {
				queryGoodsTypeList({
					parentId: '100'
				}).then(res => {
					console.log('------商品类型------', res.rows)
					this.categories = res.rows
				})
			},
			async queryGoodsList() {
				this.queryParams.pageNum = this.scroller.num
				this.queryParams.pageSize = this.scroller.size
				this.queryParams.isShow = '1'
				this.queryParams.drugType = this.drugType
				this.queryParams.shopFlag = 'normal'
				// 如果有搜索关键词，添加到查询参数中
				if (this.searchValue) {
					this.queryParams.searchValue = this.searchValue
				}else{
					this.queryParams.searchValue =''
				}
				const {
					rows,
					total
				} = await queryGoodsList(this.queryParams)
				this.products = this.products.concat(rows);
				console.log(this.products)
				this.$forceUpdate();
				this.scroller.endBySize(rows.length, total);
				uni.stopPullDownRefresh();
				uni.hideLoading();
			}
		}
	}
</script>

<style scoped>
	.shop-container {
		padding: 20px;
		background-color: #fff;
	}

	.title {
		font-size: 20px;
		font-weight: bold;
	}

	.more-icon {
		font-size: 20px;
		transform: rotate(90deg);
	}

	.search-bar {
		margin-bottom: 15px;
		/* padding: 10px 15px; */
	}

	.search-input-wrapper {
		position: relative;
		display: flex;
		align-items: center;
		background-color: #f1f1f1;
		border-radius: 5px;
		padding: 8px 15px;
	}

	.search-icon {
		font-size: 16px;
		color: #999;
		margin-right: 8px;
		width: 16px;
		height: 16px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.search-bar input {
		width: 100%;
		border: none;
		background: transparent;
		font-size: 14px;
		color: #333;
		outline: none;
	}

	.search-bar input::placeholder {
		color: #999;
	}

	.category-nav {
		margin-bottom: 20px;
		width: 100%;
	}

	.scroll-view {
		white-space: nowrap;
		width: 100%;
	}

	.category-item {
		display: inline-block;
		padding: 8px 16px;
		font-size: 14px;
		color: #666;
		position: relative;
	}

	.category-item.active {
		color: #333;
		font-weight: bold;
	}

	.category-item.active::after {
		content: '';
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 20px;
		height: 3px;
		background-color: #7ad1a0;
		border-radius: 100px 100px 0 0;
		box-shadow: none;
	}

	.product-grid {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 15px;
	}

	.product-item {
		background: #fff;
		border-radius: 4px;
		overflow: hidden;
	}

	.product-item img {
		width: 100%;
		/* height: 200px; */
		object-fit: cover;
	}

	.product-info {
		padding: 10px;
	}

	.product-title {
		font-size: 14px;
		margin-bottom: 5px;
		color: #333;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.product-specs {
		font-size: 12px;
		color: #999;
		margin: 4px 0 8px 0;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
		line-height: 1.4;
	}

	.price-sales-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.product-price {
		color: #ff4d4f;
		font-size: 16px;
		font-weight: bold;
	}

	.product-sales {
		font-size: 12px;
		color: #999;
	}
</style>